﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;


namespace NougakudoCompanion.Commons
{
    static internal class BlobStorageHelper
    {
        static CloudBlobClient client;
        static CloudBlobContainer container;

        static Tuple<bool, string> GetBlobCotainer(Environment environment)
        {
            try
            {
                CloudStorageAccount account = null;
                if (environment.UseDevelopmentFabric)
                {
                    account = CloudStorageAccount.DevelopmentStorageAccount;
                }
                else
                {
                    account = new CloudStorageAccount(
                                   new StorageCredentialsAccountAndKey(environment.Account, environment.SharedKey), false);
                }
                client = account.CreateCloudBlobClient();
                container = client.GetContainerReference(environment.BlobContainer);
                container.CreateIfNotExist();
                return new Tuple<bool,string>(true, "");
            }
            catch (Exception e)
            {
                return new Tuple<bool,string>(false, e.Message);
            }
        }

        internal static Tuple<bool, string> DownloadBlob(Environment environment, string reference, string file)
        {
            try
            {
                var ret = GetBlobCotainer(environment);
                if (!ret.Item1)
                    return ret;
                var blob = container.GetBlobReference(reference);
                // not found blob, then occur StorageClientException. and create a 0 size file.
                // Then a zero size file can't delete in this method.
                // If a destination folder don't exist, occur file I/O exception.
                blob.DownloadToFile(file);
                return new Tuple<bool, string>(true, "");
            }
            catch (StorageClientException e)
            {
                return new Tuple<bool, string>(false, e.Message);
            }
            catch (Exception e)
            {
                return new Tuple<bool, string>(false, e.Message);
            }
        }


        internal static Tuple<bool, string> UploadBlob(Environment environment, string reference, string file)
        {
            try
            {
                var ret = GetBlobCotainer(environment);
                if (!ret.Item1)
                    return ret;
                var blob = container.GetBlobReference(reference);

                blob.UploadFile(file);
                return new Tuple<bool, string>(true, "");
            }
            catch (StorageClientException e)
            {
                return new Tuple<bool, string>(false, e.Message);
            }
            catch (Exception e)
            {
                return new Tuple<bool, string>(false, e.Message);
            }

        }

        internal static Tuple<bool, string> DeleteBlob(Environment environment, string reference)
        {
            try
            {
                var ret = GetBlobCotainer(environment);
                if (!ret.Item1)
                    return ret;
                var blob = container.GetBlobReference(reference);

                var result = blob.DeleteIfExists();

                return new Tuple<bool, string>(result, "");
            }
            catch (Exception e)
            {
                return new Tuple<bool, string>(false, e.Message);
            }
        }
    }
}